var b1race = [],b2race = [],c1race = [],c2race = [];
var dataJSON = [];

$().ready(function() {
	$("#type").val( $("#types").val() );

	$("#timePeriodBt").val($("#timePeriod").val())

	validateRule();
	$.ajax({
		cache : true,
		type : "get",
		url : "/jzApi/examinations/selExaminationComAll3",
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {

			if (data.code == 0) {

				$('#cbatchName').selectPage({
					showField: 'name',
					keyField: 'id',
					data: data.data,
					eAjaxSuccess : function(d){
						var result;
						if(d) result = d.data;
						else result = undefined;
						return result;
					},
					eSelect : function(data){
						$('#cbatchName').val(data.id);
					}
				});

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
});

// $.validator.setDefaults({
// 	submitHandler : function() {
// 		update();
// 	}
// });

function update() {

	if( b1race.length ){
		b1race = b1race.map(function (v){
			return v.id;
		})
		b1race.unshift(dataJSON.data.b1[0].id);
		b1race = b1race.join(',').replace(/,/g,';;');
		$("#b1raceInput").val(b1race);
	}

	if( b2race.length ){
		b2race = b2race.map(function (v){
			return v.id;
		})
		b2race.unshift(dataJSON.data.b2[0].id);
		b2race = b2race.join(',').replace(/,/g,';;');
		$("#b2raceInput").val(b2race);
	}

	if( c1race.length ){
		c1race = c1race.map(function (v){
			return v.id;
		})
		c1race.unshift(dataJSON.data.c1[0].id);
		c1race = c1race.join(',').replace(/,/g,';;');
		$("#c1raceInput").val(c1race);
	}

	if( c2race.length ){
		c2race = c2race.map(function (v){
			return v.id;
		})
		c2race.unshift(dataJSON.data.c2[0].id);
		c2race = c2race.join(',').replace(/,/g,';;');
		$("#c2raceInput").val(c2race);
	}

	if( $("input[name='refereeB']").val() ){
		let refereeB = $("input[name='refereeB']").val();
		refereeB = refereeB.replace(/,/g,';;');
		$("input[name='refereeB']").val(refereeB)
	}

	if( $("input[name='refereeC']").val() ){
		let refereeC = $("input[name='refereeC']").val();
		refereeC = refereeC.replace(/,/g,';;');
		$("input[name='refereeC']").val(refereeC)
	}

	$.ajax({
		cache : true,
		type : "POST",
		url : "/jzweb/examinationBatchCom/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}

		}
	});

}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			type : {
				required : true
			},
			serial : {
				required : true
			},
			startTime : {
				required : true
			},
			endTime : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			},
			type : {
				required : icon + "请选择竞赛方式"
			},
			serial:{
				required : icon + "请输入项目编号"
			},
			startTime:{
				required : icon + "请选择考试开始时间"
			},
			endTime:{
				required : icon + "请选择考试结束时间"
			}
		}
	})
}

layui.use(['laydate','xmSelect'],function (){
	var laydate = layui.laydate;
	var xmSelect = layui.xmSelect;

	laydate.render({
		elem: '#startTime'
		,type: 'datetime'
	});
	laydate.render({
		elem: '#endTime'
		,type: 'datetime'
	});

	xmSelectData();

})

async function xmSelectData(){
	dataJSON = await knowledg();
	var daArr = ['b1','b2','c1','c2'];

	var b1raceInput = $("#b1raceInput").val().split(';;').splice(1,1);
	// var b2raceInput = $("#b2raceInput").val().split(';;').splice(1,1);
	var c1raceInput = $("#c1raceInput").val().split(';;').splice(1,1);
	// var c2raceInput = $("#c2raceInput").val().split(';;').splice(1,1);
	var InputArr = [b1raceInput,c1raceInput];
	var repl;

	const b1 = [{name:'婴幼儿保育技能实操',selected:true}], b2 = [{name:'模拟教学',selected:true}] , c1 = [{name:'婴幼儿早期学习支持',selected:true}], c2 = [{name:'集体教学活动设计',selected:true}], c3 = [{name:'说课',selected:true}];

	daArr.forEach(function (response,idx){
		repl = InputArr[idx];
		dataJSON.data[response].forEach(function (v,s){
			if( v.id == repl[s] ){
				v.selected = true;
			}else{
				v.selected = false;
			}
			v.children.forEach(function (val,k){
				if( repl[k] == val.id ){
					val.selected = true;
				}else{
					val.selected = false;
				}
			})
		})
	});

	//选择模块题目
	xmSelect.render({
		el: '#b1race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.b1
			return b1;
		},
		on:function (data){
			b1race = data.arr;
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			arr.unshift(dataJSON.data.b1[0].id)
			$("#b1raceInput").val( arr.join(',').replace(/,/,';;') );
		}
	})

	xmSelect.render({
		el: '#b2race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.b2
			return b2
		},
		on:function (data){
			b2race = data.arr
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			arr.unshift(dataJSON.data.b2[0].id)
			$("#b2raceInput").val( arr.join(',').replace(/,/,';;') );
		}
	})

	xmSelect.render({
		el: '#c1race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.c1
			return c1
		},
		on:function (data){
			c1race = data.arr
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			arr.unshift(dataJSON.data.c1[0].id)
			$("#c1raceInput").val( arr.join(',').replace(/,/,';;') );
		}
	})

	xmSelect.render({
		el: '#c2race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.c2
			return c2
		},
		on:function (data){
			c2race = data.arr
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			arr.unshift(dataJSON.data.c2[0].id)
			$("#c2raceInput").val( arr.join(',').replace(/,/,';;') );
			// b1race = data.arr
		}
	})

	xmSelect.render({
		el: '#c3race',
		autoRow: true,
		paging: true,
		pageSize: 20,
		height: '300px',
		name:'',
		tree: {
			show: true,
			showFolderIcon: true,
			showLine: true,
			indent: 20,
			expandedKeys: [ -1 ],
			clickExpand:true,
			clickCheck: true
		},
		data(){
			// return dataJSON.data.c2
			return c3
		},
		on:function (data){
			c2race = data.arr
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			arr.unshift(dataJSON.data.c2[0].id)
			$("#c2raceInput").val( arr.join(',').replace(/,/,';;') );
			// b1race = data.arr
		}
	})
}

function knowledg(){
	return $.ajax({
		cache : true,
		type : "get",
		url : "/jzweb/itemBank/byKnowledgesGet",
		data : {
			cprofileId:'1654692612624175105'
		},
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				return data.data
				// parent.layer.msg("操作成功");
				// parent.reLoad();
				// var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				// parent.layer.close(index);
			} else {
				parent.layer.alert(data.msg)
			}

		}
	});
}


examJudge();
function examJudge(){

	// $("#refereeBBox").click(function (ev){
	let examferrBs = $(".examferrB").val();
		examferrBs = examferrBs.split(';;');
	let xelBox = xmSelect.render({
		el: '#refereeBBox',
		autoRow: true,
		name: '',
		initValue: examferrBs ,
		max: 5,
		data() {
			return []
		},
		maxMethod(seles, item) {
			layer.msg(`最多只能选择5个`)
		},
		on: function (data) {
			let arr = [];
			data.arr.map(function (v){
				arr.push(v.id);
			})
			$(".examferrB").val(arr.join(',').replace(/,/,';;'));
		}
	})

	$.ajax({
		cache: true,
		type: "get",
		url: "/jzweb/examinationBatchCom/getUserReferee",
		data: {
			track: 'A'
		},
		async: false,
		error: function (request) {
			parent.layer.alert("Connection error");
		},
		success: function (data) {

			if (data.code == 0) {
				var format = data.data;
					format.forEach(function (v, k) {
						v.selected = true;
						v.value = v.id
					})

					xelBox.update({
						data: format,
						autoRow: true,
					})
			}
		}
	})

	// $("#refereeCBox").click(function (){
	$.ajax({
		cache: true,
		type: "get",
		url: "/jzweb/examinationBatchCom/getUserReferee",
		data: {
			track: 'C'
		},
		async: false,
		error: function (request) {
			parent.layer.alert("Connection error");
		},
		success: function (data) {

			if (data.code == 0) {
				var format = data.data;
				let examferrC = $(".examferrC").val();
					examferrC = examferrC.split(';;');
					format.forEach(function (v, k) {
						v.selected = true;
						v.value = v.id
					})

				xmSelect.render({
					el: '#refereeCBox',
					autoRow: true,
					name: '',
					max: 5,
					initValue: examferrC ,
					data() {
						return format
					},
					maxMethod(seles, item) {
						layer.msg(`最多只能选择5个`)
					},
					on: function (data) {
						let arr = [];
						data.arr.map(function (v){
							arr.push(v.id);
						})
						$(".examferrC").val( arr.join(',').replace(/,/,';;') );
					}
				})

			}
		}
	})
	// })

}